﻿<%@ WebHandler Language="C#" Class="ContainerQualityCollectHandler" %>

/*Description: 质量提起处理程序
'Copyright (c) : 通力凯顿（北京）系统集成有限公司
'Writer: Zhangrj 
'create Date:2022-10-08
'Rewriter:
'Rewrite Date:
*/
using System;
using System.Web;
using BusinessRule.DTO;
using System.Web.SessionState;
using DatabaseAccess.DBUtility;
using System.Data.OracleClient;
using System.Data;
using Newtonsoft.Json;
using System.Collections.Generic;
using BusinessRule.Business;
using BusinessRule.IBusiness;
using BusinessRule.BusinessFactory;

public class ContainerQualityCollectHandler : IHttpHandler, IRequiresSessionState
{

    CurrentUserInfo m_UserInfo;
    QulityCheckBusiness Q_check = new QulityCheckBusiness();
    uLMSCommonBusiness commonBusiness = new uLMSCommonBusiness();
    uLMSCheckIBusiness checkBusiness = uMESBusinessFactory.instance.GetuLMSCheckObject();
    public void ProcessRequest(HttpContext context)
    {
        try
        {
            var action = context.Request["action"].ToString();
            m_UserInfo = (CurrentUserInfo)context.Session["CurrentUserInfo"];
            if (m_UserInfo == null)
            {
                context.Response.Write(JsonConvert.SerializeObject(new uMESProcessResult() { Result = false, Message = "数据过期，请重新登录" }));
                return;
            }
            switch (action)
            {
                case "GetInitPageData": //初始化页面数据
                    GetInitPageData(context);
                    break;
                case "GetEmployee": //获取责任人
                    GetEmployee(context);
                    break;
                case "SaveQulityCollectData": //保存质量提起
                    SaveQulityCollectData(context);
                    break;
                case "GetQualityCollectExamine": //获取质量提起待审核数据
                    GetQualityCollectExamine(context);
                    break;
                case "SaveQualityCollectExamine": //保存质量提起待审核数据
                    SaveQualityCollectExamine(context);
                    break;
                default:
                    break;
            }
        }
        catch (Exception ex)
        {
            var result = new uMESProcessResult();
            result.Result = false;
            result.Message = ex.Message;
            context.Response.ContentType = "text/html";
            context.Response.Write(JsonConvert.SerializeObject(result));
        }
    }
    /// <summary>
    /// 获取质量提起初始化数据
    /// </summary>
    /// <param name="context"></param>
    public void GetInitPageData(HttpContext context)
    {
        DataSet ds = new DataSet();
        uMESProcessResult result = new uMESProcessResult() { Result = true };
        DataTable dt = (DataTable)context.Session["QualityCollectDatas"];
        dt.TableName = "dtSpec";
        ds.Tables.Add(dt);
        //获取件号信息
        Dictionary<string, string> dic = new Dictionary<string, string>();
        dic.Add("productID", dt.Rows[0]["ProductID"].ToString());
        dic.Add("togetherFlag", dt.Rows[0]["TogatherFlag"].ToString());
        dic.Add("mfgOrderName", dt.Rows[0]["MfgOrderName"].ToString());
        DataTable productDt = checkBusiness.GetQualityCollectProductInfo(dic);
        productDt.TableName = "productDt";
        ds.Tables.Add(productDt);
        //获取分厂
        DataTable dtFactory = commonBusiness.GetFactory(new Dictionary<string, string>());
        dtFactory.TableName = "dtFactory";
        ds.Tables.Add(dtFactory);
        result.DBDataSet = ds;
        context.Response.Write(JsonConvert.SerializeObject(result));
    }

    /// <summary>
    /// 获取员工
    /// </summary>
    /// <param name="context"></param>
    public void GetEmployee(HttpContext context)
    {
        string factoryID = context.Request["factoryID"].ToString();
        DataTable dt = commonBusiness.GetEmployee(null, factoryID);
        uMESProcessResult result = new uMESProcessResult() { Result = true };
        result.DBTable = dt;
        context.Response.Write(JsonConvert.SerializeObject(result));
    }

    /// <summary>
    /// 保存质量提交数据
    /// </summary>
    /// <param name="context"></param>
    public void SaveQulityCollectData(HttpContext context)
    {
        DataTable paramDt = JsonConvert.DeserializeObject<DataTable>(context.Request["data"].ToString());
        if (paramDt.Rows.Count == 0)
        {
            context.Response.Write(JsonConvert.SerializeObject(new uMESProcessResult() { Result = false, Message = "没有选择批次" }));
            return;
        }
        paramDt.Columns.Add("Sequence");//顺序号
        paramDt.Columns.Add("DealName");//处置单号
        DataRow dr = paramDt.Rows[0];
        var maxSequence = Q_check.GetQulityQuestionDeal_MaxSequence();
        dr["Sequence"] = maxSequence;
        var strSequence = "000000" + maxSequence;
        var dealName = "WT-" + DateTime.Now.ToString("yyyy") + "-" + m_UserInfo.FactoryCode + "-" + strSequence.Substring(strSequence.Length - 4);
        dr["DealName"] = dealName;
        uMESProcessResult result = new uMESProcessResult() { Result = true };
        result = checkBusiness.SaveQulityCollectData(dr, m_UserInfo);
        context.Response.Write(JsonConvert.SerializeObject(result));
    }

    /// <summary>
    /// 获取质量提起审核数据
    /// </summary>
    /// <param name="context"></param>
    public void GetQualityCollectExamine(HttpContext context)
    {
        DataTable dt = JsonConvert.DeserializeObject<DataTable>(context.Request["data"].ToString());
        if (dt.Rows.Count == 0)
        {
            context.Response.Write(JsonConvert.SerializeObject(new uMESProcessResult() { Result = false, Message = "没有选择批次" }));
            return;
        }
        uMESProcessResult result = new uMESProcessResult() { Result = true };
        var para = new Dictionary<string, string>();
        para.Add("dealname", dt.Rows[0]["dealname"].ToString());//处置单号
        para.Add("mfgordername", dt.Rows[0]["ordername"].ToString());//订单号
        para.Add("productname", dt.Rows[0]["productname"].ToString());//件号
        para.Add("startedate", dt.Rows[0]["startedate"].ToString());//开始日期
        para.Add("enddate", dt.Rows[0]["enddate"].ToString());//结束日期
        para.Add("status", dt.Rows[0]["selStatus"].ToString());//审核状态
        para.Add("factoryID", m_UserInfo.FactoryID);//责任分厂
        result.DBTable = checkBusiness.GetQualityCollectExamineData(para);
        context.Response.Write(JsonConvert.SerializeObject(result));
    }


    /// <summary>
    /// 保存质量提起审核
    /// </summary>
    /// <param name="context"></param>
    public void SaveQualityCollectExamine(HttpContext context)
    {
        string qualityIDs = context.Request["qualityIDs"].ToString();
        Dictionary<string, string> dic = new Dictionary<string, string>();
        dic.Add("qualityIDs", qualityIDs.Trim(','));
        dic.Add("confirmEmployeeID", m_UserInfo.EmployeeID.ToString());
        uMESProcessResult result = new uMESProcessResult() { Result = true };
        result.Result = checkBusiness.SaveQualityCollectExamineData(dic);
        result.Message = result.Result ? "审核成功" : "审核失败";
        context.Response.Write(JsonConvert.SerializeObject(result));
    }



    public bool IsReusable
    {
        get
        {
            return false;
        }
    }

}